package cn.csg.iotplatform.module.system.service.logger;

import cn.csg.iotplatform.framework.web.core.util.WebFrameworkUtils;
import cn.csg.iotplatform.module.system.dal.dataobject.logger.GkDeviceCenterApiLogDO;
import cn.csg.iotplatform.module.system.dal.mysql.logger.GkDeviceCenterApiLogMapper;
import cn.csg.iotplatform.module.system.enums.DeviceCenterApiEnum;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 共库设备中心接口调用日志 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class GkDeviceCenterApiLogServiceImpl implements GkDeviceCenterApiLogService {

    @Value("${iot.eqt-center.address}")
    public String ApiUrl;

    @Resource
    private GkDeviceCenterApiLogMapper gkDeviceCenterApiLogMapper;

    @Override
    public void createLog(GkDeviceCenterApiLogDO gkDeviceCenterApiLogDO, DeviceCenterApiEnum deviceCenterApiEnum) {

        //插入日志
        gkDeviceCenterApiLogDO.setApiType(deviceCenterApiEnum.getApiType());
        gkDeviceCenterApiLogDO.setOptTime(LocalDateTime.now());
        gkDeviceCenterApiLogDO.setRemark( deviceCenterApiEnum.getApiDesc() );
        gkDeviceCenterApiLogDO.setApiUrl( ApiUrl + deviceCenterApiEnum.getApiUrl() );

        Long loginUserId = WebFrameworkUtils.getLoginUserId();
        gkDeviceCenterApiLogDO.setOptUserId( ObjectUtil.isNotEmpty( loginUserId ) ? loginUserId.toString() : null );

        gkDeviceCenterApiLogMapper.insert( gkDeviceCenterApiLogDO );
    }


}